home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 501-525 / disk_512 / csh / csh515s.lzh / run.c < prev    next >
C/C++ Source or Header  |  1991-06-23  |  5KB  |  263 lines

  1.  
  2. /*
  3.  * RUN.C
  4.  *
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  *    RUN   handles running of external commands.
  8.  *
  9.  * Version 2.07M by Steve Drew 10-Sep-87
  10.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  11.  * Version 5.00L by Urban Mueller 17-Feb-91
  12.  *
  13.  */
  14.  
  15. #include "shell.h"
  16.  
  17. int MySyncRun( char *com, char *args, BPTR in, BPTR out, int nosync );
  18. int echofunc(void);
  19.  
  20. int
  21. do_run( char *str, int nosync )
  22. {
  23.     int retcode;
  24.     char buf[200];        /* enough space for 100 char cmd name + path stuff */
  25.     char *path, *argline, *copy, *ext, *end;
  26.  
  27.     if( !*av[0] )
  28.         return 0;
  29.  
  30.     if( (retcode=echofunc())>=0 )
  31.         return retcode;
  32.  
  33.     a0tospace( av[0] );                                 /* allow "com mand" */
  34.  
  35.     argline=compile_av(av, 1, ac, ' ', 1);
  36.  
  37.     if (strlen(av[0]) > 100) { ierror(NULL,509); return -1; }
  38.  
  39.     if( !strcmp(av[0],".bra") || !strcmp(av[0],".ket") ) return 0;
  40.  
  41.     if( ac==1 && isdir(av[0])) {
  42.         sprintf(buf,"cd %s",av[0]);
  43.         return execute( buf );
  44.     }
  45.  
  46.     IoError=IoErr();
  47.     if( (IoError==218 || IoError==225 || IoError==226) && index(av[0],':')) {
  48.         ierror( av[0], IoError );
  49.         return 20;
  50.     }
  51.  
  52.     sprintf(buf,"res_%s",BaseName(av[0]));               /* delayed residents */
  53.     if (o_resident && Getenv(buf, buf+100, 90L)) {
  54.         Setenv(buf,NULL);
  55.         loadres(buf+100);
  56.     }
  57.  
  58.     if( (retcode=MySyncRun(av[0],argline,0,0,nosync))>=0 )   /* AmigaDOS path */
  59.         goto done2;
  60.  
  61.     if( retcode==PR_NOMEM ) {
  62.         ierror( av[0], 103 );
  63.         return 20;
  64.     }
  65.  
  66.     IoError=IoErr();
  67.     if( (IoError==218 || IoError==225 || IoError==226) && index(av[0],':')) {
  68.         ierror( av[0], IoError );
  69.         return 20;
  70.     }
  71.  
  72.     if (path = dofind(av[0],"",buf+80,v_path)) {             /* shell path    */
  73.         struct DPTR *dp;
  74.         BPTR fh;
  75.         int stat, script;
  76.         if((retcode = MySyncRun(path,argline,0,0,nosync))>=0)
  77.             goto done2;
  78.         if(dp=dopen(path,&stat)) {
  79.             script= dp->fib->fib_Protection&FIBF_SCRIPT;
  80.             dclose(dp);
  81.             if( !stat && script ) {
  82.                 char *t;
  83.                 buf[0]=0;
  84.                 if( fh=Open(path,MODE_OLDFILE )) {
  85.                     Read(fh,buf,79);
  86.                     Close(fh);
  87.                     if(t=index(buf,'\n')) *t=0;
  88.                 }
  89.                 t=buf+2;
  90.                 if( (buf[0]!='/' || buf[1]!='*') &&
  91.                      ((buf[0]!=';' && buf[1]!='#') || buf[1]!='!'))
  92.                     strcpy(t,"Execute");
  93.                 strcat(t," ");
  94.                 memmove(t+strlen(t),buf+80,strlen(buf+80)+1);
  95.                 execute(t);
  96.                 return 0;
  97.             }
  98.         }
  99.     }
  100.  
  101.     if(!(end=rindex(av[0],'.'))) end="";               /* automatic sourcing */
  102.     ext=strcmp(end,".sh") ? ".sh" : "";
  103.     if (path = dofind(av[0],ext,buf,v_path)) {
  104.         av[1] = buf;
  105.         copy = salloc(strlen(str)+3);
  106.         sprintf(copy,"x %s",str);
  107.         retcode = do_source(copy);
  108.         goto done;
  109.     }
  110.  
  111.     copy=salloc(strlen(av[0])+strlen(argline)+5);
  112.     sprintf(copy,"%s %s",av[0],argline);
  113.  
  114.     ext=strcmp(end,".rexx") ? ".rexx" : "";           /* automatic rx-ing   */
  115.     if( path = dofind(av[0], ext, buf, v_rxpath )) {
  116.         if( (retcode=MySyncRun("rx",copy,0,0,nosync)) >=0 ) goto done;
  117.         if (path = dofind("rx","",buf,v_path)) {
  118.             retcode = MySyncRun(path,copy,0,0,nosync);
  119.             goto done;
  120.         }
  121.     }
  122.  
  123.     if( !doaction(av[0],"exec",argline)) {
  124.         retcode=0;
  125.         goto done;
  126.     }
  127.  
  128.     retcode=-1;
  129.     fprintf(stderr,"Command not found %s\n",av[0]);
  130.  
  131. done:
  132.     free( copy );
  133. done2:
  134.     setioerror( IoErr() );
  135.     free( argline );
  136.     return retcode;
  137. }
  138.  
  139. struct Segment {
  140.     BPTR NextEntry;
  141.     LONG UseCount;
  142.     BPTR SegPtr;
  143.     BSTR SegName;
  144. };
  145.  
  146. int
  147. MySyncRun( char *com, char *args, BPTR in, BPTR out, int nosync )
  148. {
  149.     struct Segment *seg;
  150.     int ret, len=strlen(args);
  151.     char buf2[200], *buf=buf2;
  152.     long oldname;
  153.  
  154. #ifdef KICK20
  155.     if( o_kick20 ) {
  156.         oldname = (long)Mycli->cli_CommandName;
  157.  
  158.         args[len]='\n'; args[len+1]=0;
  159.  
  160.         while( (long)buf & 3 ) buf++;
  161.         buf[0] = strlen( com );
  162.         strncpy(buf+1,com,80);
  163.  
  164.         Forbid();
  165.         if(seg=FindSegment( (UBYTE *)com, NULL, 0 ))
  166.             seg->UseCount++;
  167.         Permit();
  168.         if( seg ) {
  169.             Mycli->cli_CommandName = (long)buf/4;
  170.  
  171.             ret=RunCommand(seg->SegPtr, Mycli->cli_DefaultStack,
  172.                                        (UBYTE *)args, strlen(args));
  173.             seg->UseCount--;
  174.             Mycli->cli_CommandName = (long)oldname;
  175.             return ret;
  176.         }
  177.  
  178.         if( o_internal ) {
  179.             Forbid();
  180.             seg=FindSegment( (UBYTE *)com, NULL, 1 );
  181.             Permit();
  182.             if( seg ) {
  183.                 Mycli->cli_CommandName = (long)buf/4;
  184.  
  185.                 ret=RunCommand(seg->SegPtr, Mycli->cli_DefaultStack,
  186.                                            (UBYTE *)args, strlen(args));
  187.                 Mycli->cli_CommandName = (long)oldname;
  188.                 return ret;
  189.             }
  190.         }
  191.  
  192.         args[len]=0;
  193.  
  194.     }
  195. #endif
  196.  
  197.     if( nosync ) {
  198.         sprintf(buf2,"%s %s",com,args);
  199.         Execute(buf2,0,Myprocess->pr_COS);
  200.         return 0;
  201.     }
  202.  
  203.     if( (ret= SyncRun( com, args, in, out ))>=0 )
  204.         return ret;
  205.  
  206.     return -1;
  207. }
  208.  
  209. #if 0
  210. int
  211. do_which( char *str )
  212. {
  213.     char *got, *com=av[1];
  214.  
  215.     if( get_var(LEVEL_ALIAS,com) ) {
  216.         printf("Shell Alias '%s'\n",com);
  217.         return 0;
  218.     }
  219.  
  220.     if( *(got=find_internal( com ))>1 ) {
  221.         printf("Shell Internal '%s'\n",got);
  222.         return 0;
  223.     }
  224.  
  225.  
  226.  
  227.     printf( "Not found\n" );
  228.     return 20;
  229. }
  230. #endif
  231.  
  232.  
  233. char *
  234. dofind( char *cmd, char *ext, char *buf, char *path)
  235. {
  236.     char *ptr, *s=path, *ret=NULL;
  237.  
  238.     Myprocess->pr_WindowPtr = (APTR)(-1);
  239.     sprintf(buf,"%s%s",cmd,ext);
  240.     if (exists(buf)) {
  241.         ret=buf;
  242.         goto terminate;
  243.     }
  244.     if (BaseName(buf)==buf) {
  245.         if( *path=='_' )
  246.             s = get_var(LEVEL_SET, path);
  247.         while (*s) {
  248.             for (ptr=buf; *s && *s!=','; ) *ptr++ = *s++;
  249.             if( ptr[-1]!=':' && ptr[-1]!='/')
  250.                 *ptr++='/';
  251.             sprintf(ptr, "%s%s", cmd, ext);
  252.             if (exists(buf)) {
  253.                 ret=buf;
  254.                 goto terminate;
  255.             }
  256.             if (*s) s++;
  257.         }
  258.     }
  259. terminate:
  260.     Myprocess->pr_WindowPtr = (APTR)o_noreq;
  261.     return ret;
  262. }
  263.